#include "addfinancesubject.h"
#include "ui_addfinancesubject.h"
#include <QDebug>

AddFinanceSubject::AddFinanceSubject(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddFinanceSubject)
{
    ui->setupUi(this);

    setWindowTitle("添加科目");

    connect(&financeService, &FinanceService::addFinanceItemSuccess,
            [=]()
    {
        emit this->addFinanceItemSuccess();
        this->close();
    });
}

AddFinanceSubject::~AddFinanceSubject()
{
    delete ui;
}

void AddFinanceSubject::setDirectory(QList<QStringList> directory){
    if(directory.size()!=3){
        return;
    }
    this->directory.clear();
    this->directory.append(directory[1]);
    this->directory.append(directory[2]);
    ui->comboBoxPrimaryDirectory->setCurrentIndex(0);
    ui->comboBoxSecondaryDirecctory->clear();
    ui->comboBoxSecondaryDirecctory->addItems(this->directory[0]);
    ui->lineEditSubject->clear();
}

void AddFinanceSubject::setCurrentDirectory(QString primaryDirectory, QString secondaryDirectory){
    if(primaryDirectory == "收入"){
        ui->comboBoxPrimaryDirectory->setCurrentIndex(0);
        if(secondaryDirectory.isEmpty() || secondaryDirectory == "所有收入"){
            ui->comboBoxSecondaryDirecctory->setCurrentIndex(0);
        }else{
            ui->comboBoxSecondaryDirecctory->setCurrentText(secondaryDirectory);
        }
    }else if(primaryDirectory == "支出"){
        ui->comboBoxPrimaryDirectory->setCurrentIndex(1);
        if(secondaryDirectory.isEmpty() || secondaryDirectory == "所有支出"){
            ui->comboBoxSecondaryDirecctory->setCurrentIndex(0);
        }else{
            ui->comboBoxSecondaryDirecctory->setCurrentText(secondaryDirectory);
        }
    }
}

void AddFinanceSubject::on_pushButtonCancel_clicked()
{
    this->close();
}

void AddFinanceSubject::on_comboBoxPrimaryDirectory_currentIndexChanged(int index)
{
    ui->comboBoxSecondaryDirecctory->clear();
    ui->comboBoxSecondaryDirecctory->addItems(directory[index]);
}

void AddFinanceSubject::on_pushButtonAdd_clicked()
{
    financeService.addFinanceItem(
                this,
                ui->pushButtonAdd,
                ui->comboBoxPrimaryDirectory->currentIndex(),
                ui->comboBoxSecondaryDirecctory->currentText(),
                ui->lineEditSubject->text());
}
